using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class Rook : SuperEnemies//破城兵
{
    private void FixedUpdate()
    {
        if (HP <= 0)
        {
            Destroy(gameObject);
            manager.ChangedEnemyVal();
        }
    }
    protected override void PathFinding()
    {

        if (GameObject.FindWithTag("wall") != null)
        {
            GameObject[] walls = GameObject.FindGameObjectsWithTag("wall");//将城墙tag设置为wall，查找现在是否有城墙
            GameObject closestWall = null;
            float closestDistance = Mathf.Infinity;//将最初的城墙距离设置为无限     
            Vector2 curDistance = transform.position;
            foreach (var wall in walls)
            {
                float distance = Vector2.Distance(wall.transform.position, curDistance);
                if (distance < closestDistance)//找到距离最近的城墙
                {
                    closestDistance = distance;
                    closestWall = wall;
                }
            }
            agent.SetDestination(closestWall.transform.position);
        }
        else
        {
            base.PathFinding();//如果没有找到城墙对象，那么进行原先的寻路
        }
    }
    protected override IEnumerator Attack(TowerHolder tower)
    {
        yield return new WaitForSeconds(AttackSpeed);
        if (tower.GameObject().CompareTag("wall"))
        {
            tower.GetHurt(damage * 2);
        }
        else
        {
            tower.GetHurt(damage);
        }
    }
}
